//*****************************************************************************
//
// distanceIR_task.c - Control library for the distance IR sensor on the evalbot
// I added the analog input to pin PE7 because he was isolated.
//
//*****************************************************************************

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"


void DistanceIRInit()
{
    // Enable the ADC0 interface...it matches the E7 pin
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    // Select the analog ADC function for these pins.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_7);

    // Enable sample sequence 3 with a processor signal trigger. And
    // configure it for step 0 sequence 3. This is selected cause I
    // only need to check it once.
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);

    // Since sample sequence 3 is now configured, it must be enabled.
    ADCSequenceEnable(ADC0_BASE, 3);

    // Clear the interrupt status flag.
    ADCIntClear(ADC0_BASE, 3);
}

unsigned long ReadIRDistanceValue()
{
	unsigned long conversionBuffer[1];

    // Trigger the ADC conversion.
    ADCProcessorTrigger(ADC0_BASE, 3);

    // Wait for conversion to be completed.
    while(!ADCIntStatus(ADC0_BASE, 3, false))
    {
    }

    // Read ADC Value.
    ADCSequenceDataGet(ADC0_BASE, 3, conversionBuffer);

    return conversionBuffer[0];
}



